package com.healthyeating.manage.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.healthyeating.common.exception.ServiceException;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.domain.Recipe;
import com.healthyeating.manage.domain.dto.ConsultRecipeDto;
import com.healthyeating.manage.mapper.RecipeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.ConsultMapper;
import com.healthyeating.manage.domain.Consult;
import com.healthyeating.manage.service.IConsultService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 咨询Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-04-07
 */
@Service
public class ConsultServiceImpl implements IConsultService {

    @Autowired
    private ConsultMapper consultMapper;

    @Autowired
    private RecipeMapper recipeMapper;

    /**
     * 查询咨询
     *
     * @param consultId 咨询主键
     * @return 咨询
     */
    @Override
    public Consult selectConsultByConsultId(Long consultId) {
        return consultMapper.selectConsultByConsultId(consultId);
    }

    /**
     * 查询咨询列表
     *
     * @param consult 咨询
     * @return 咨询
     */
    @Override
    public List<Consult> selectConsultList(Consult consult) {
        return consultMapper.selectConsultList(consult);
    }

    /**
     * 新增咨询
     *
     * @param consultRecipeDto 咨询
     * @return 结果
     */
    @Override
    @Transactional
    public int insertConsult(ConsultRecipeDto consultRecipeDto) {
        Consult _consult = new Consult();
        _consult.setOldRecipeId(consultRecipeDto.getRecipe().getRecipeId());
        List<Consult> consults = consultMapper.selectConsultList(_consult);
        if (!consults.isEmpty()) {
            throw new ServiceException("已经处理过该咨询了");
        }
        Recipe recipe = new Recipe();
        BeanUtil.copyProperties(consultRecipeDto.getRecipe(), recipe);
        recipe.setBreakfastRecipe(consultRecipeDto.getNewBreakFastRecipe());
        recipe.setLunchRecipe(consultRecipeDto.getNewLunchRecipe());
        recipe.setDinnerRecipe(consultRecipeDto.getNewDinnerRecipe());
        recipe.setConsultFlag(2);
        recipeMapper.insertRecipe(recipe);
        Consult consult = consultRecipeDto.getConsult();
        consult.setNewRecipeId(recipe.getRecipeId());
        consult.setDietitianId(SecurityUtils.getUserId());
        return consultMapper.insertConsult(consult);
    }

    /**
     * 修改咨询
     *
     * @param consult 咨询
     * @return 结果
     */
    @Override
    public int updateConsult(Consult consult) {
        return consultMapper.updateConsult(consult);
    }

    /**
     * 批量删除咨询
     *
     * @param consultIds 需要删除的咨询主键
     * @return 结果
     */
    @Override
    public int deleteConsultByConsultIds(Long[] consultIds) {
        return consultMapper.deleteConsultByConsultIds(consultIds);
    }

    /**
     * 删除咨询信息
     *
     * @param consultId 咨询主键
     * @return 结果
     */
    @Override
    public int deleteConsultByConsultId(Long consultId) {
        return consultMapper.deleteConsultByConsultId(consultId);
    }
}
